<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Photo extends MX_Controller {

	private $module = 'photo';
	private $table = 'photo';
	function __construct(){
		parent::__construct();
		$this->load->model($this->module.'_model','model');
		if($this->uri->segment(1)=='admincp'){
			if($this->uri->segment(2)!='login'){
				if(!$this->session->userdata('userInfo')){
					header('Location: '.base_url().'admincp/login');
					return false;
				}
			}
			$this->template->set_template('admincontrolpanel');
			$this->template->write('title','Admin Control Panel');
		}
	}
	/*------------------------------------ Admin Control Panel ------------------------------------*/
	public function admincp_index($start = 0,$limit = 10){		
		$this->load->library('pagination');
		$config['total_rows'] 	= $this->model->getTotalsearchContent();
		$config['base_url'] 	= base_url().'admincp/'.$this->module.'/index/';
		$config['per_page'] 	= $limit;
		$config['num_links'] 	= 1;
		$config['uri_segment']  = 4;
		$this->pagination->initialize($config);
		
		$result = $this->model->getsearchContent($config['per_page'],$start);
		$data = array(
			'module'		=> $this->module,
			'result'		=> $result,
			
		);
		
		$this->template->write_view('content','BACKEND/index',$data);
		$this->template->render();
	}
	
	
	public function admincp_update($id=0){
		$result[0] = array();
		if($id!=0){
			$result = $this->model->getDetailManagement($id);
		}
		
		$album_id = $this->getParent(2);
		
		$data = array(
			'result'=>$result[0],
			'module'=>$this->module,
			'album_id'	=> $album_id,
			'id'=>$id
		);
		$this->template->write_view('content','BACKEND/editContent',$data);
		$this->template->render();
	}

	public function getParent()
	{
		$level1 = $this ->model->getParentCat(2);
		
		$result = array();
		if(count($level1)>0){
			foreach($level1 as $a){
				$level2 = $this->model->getCatDetail($a->id,-1,-1);
				
				if(count($level2)>0){
					foreach($level2 as $b){
						$result[] = array('id' => $b->id,'name'=>$a->name.'_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$b->name);
					}
				}
			}
		}
		
		if($result)
		{
			return $result;
		}else{
			return false;
		}
	}
	
	public function admincp_save(){
		if($_POST){
			$fileName = '';
			$videoName = '';
			if($_FILES){
				if(isset($_FILES['image']['error'])){
					$typeFileImage = strtolower(substr($_FILES['image']['type'],0,5));
					if($typeFileImage == 'image'){
						$tmp_name = $_FILES['image']["tmp_name"];
						$file_name = $_FILES['image']['name'];
						$ext = strtolower(substr($file_name, -4, 4));
						if($ext=='jpeg'){
							$fileName = md5(time()).'_'.SEO(substr($file_name,0,-5)).'.jpg';
						}else{
							$fileName = md5(time()).'_'.SEO(substr($file_name,0,-4)).$ext;
						}
					}else{
						print 'Only upload Image.';
						exit;
					}
				}
			}
			
			$upload_path = DIR_PHOTO;
			if(!file_exists($upload_path)){
				@mkdir($upload_path, 0777);
			}

			if($this->model->saveManagement($fileName)){
				if($_FILES){
					if(isset($_FILES['image']['error'])){		
						move_uploaded_file($tmp_name,DIR_PHOTO.$fileName);
						@resize_image(DIR_PHOTO,$fileName, 200, 150, DIR_PHOTO_THUMB);
					}
				}
			}
		}
		header('Location: '.base_url().'admincp/'.$this->module);
		return false;
	}
	
	public function admincp_del_all()
	{
		if($_POST)
		{
			if($this->input->post('checkAll') == 'on'){
				$check = $this->input->post('check');
				
				for($i = 0;$i <= count($check);$i++)
				{
					$this->admincp_delete($check[$i]);
				}
			}
		}
	}
	
	public function admincp_delete($id = 0){
		if($id != 0){
			$result = $this->model->getDetailManagement($id);
			$this->db->where('id',$id);
			if($this->db->delete(PREFIX.$this->table)){
				@unlink(DIR_PHOTO.$result[0]->link);
				@unlink(DIR_PHOTO_THUMB.$result[0]->link);
				header('Location: '.base_url().'admincp/'.$this->module);
				return false;
			}
		}
	}

	public function admincp_updateStatus(){
		if($this->input->post('status')==0){
			$status = 1;
		}else{
			$status = 0;
		}
		$data = array(
			'status'=>$status
		);
		$this->db->where('id', $this->input->post('id'));
		$this->db->update(PREFIX.$this->table, $data);
		
		$update = array(
			'status'=>$status,
			'id'=>$this->input->post('id'),
			'module'=>$this->module
		);
		$this->load->view('BACKEND/updateStatus',$update);
	}
	
	/*------------------------------------ End Admin Control Panel --------------------------------*/
}